/*
 * Indiana University Community Grid Computing Lab Software License,Version 1.1
 *
 * Copyright (c) 2002 Community Grid Computing Lab, Indiana University.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *      "This product includes software developed by the Indiana University
 *       Community Grid Computing Lab (http://www.communitygrids.iu.edu/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Indiana Univeristy","Indiana Univeristy Pervasive Techonology
 *    Labs" and  "Indiana Univeristy Community Grid Computing Lab" must not be
 *    used to endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    http://www.communitygrids.iu.edu/.
 *
 * 5. Products derived from this software may not use "Indiana
 *    University" name nor may "Indiana University" appear in their name,
 *    without prior written permission of the Indiana University.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * INDIANA UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package cgl.sensorgrid.sopac.gps;

import java.util.*;

import cgl.sensorgrid.common.*;
import cgl.sensorgrid.sopac.gps.filters.*;
/**
 *
 * <p>Title: SensorGrid</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author Galip Aydin
 * @version 1.0
 */
public class RunNetwork extends Thread {

    private Hashtable networkHash;
    private java.util.Properties properties = new java.util.Properties();
    // private String networkName;
    private String nbHost;
    private String nbPort;

    public RunNetwork() {
        loadProperties();
        this.nbHost = properties.getProperty("nb.host");
        this.nbPort = properties.getProperty("nb.port");
    }


    private java.util.Properties loadProperties() {
        // Read properties file.
        try {
            properties = PropertyFile.loadProperties(
                    "sensorgrid.properties");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return properties;
    }


    public void runNetwork(String networkName) {
        try {
//            GetStationsRSS rss = new GetStationsRSS();
//            networkHash = rss.getNetworkInfo(networkName);

            //1. run RYO forwarder
            rtd2nb(networkName);
            System.out.println("\n\nSleep for 2 seconds \n\n");
            sleep(3000);

            //2. run positions filter
            //ryo2pos(networkName);
            System.out.println("\n\nSleep for 2 seconds \n\n");
            sleep(2000);

            //3. start single station filters
//            singleStations();
//            System.out.println("\n\nSleep for 2 seconds \n\n");
//            sleep(2000);

            //4. run displacements filters
            //calcDisplacements();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /**
     *
     */
    public void rtd2nb(String networkName) {
        try {
            GetStationsRSS get = new GetStationsRSS();
            Hashtable hash = get.networkInfo(networkName);
            String ip = (String) hash.get("ip");
            String port = (String) hash.get("port");

            String ryoTopic = "/SOPAC/GPS/" + networkName +
                              "/RYO";

//            System.out.println("** ip = " + ip);
//            System.out.println("** port = " + port);
//            Rtd2Narada rtd = new Rtd2Narada(ip,
//                                            Integer.parseInt(port),
//                                            nbHost, nbPort, ryoTopic);
//            rtd.start();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     *
     */
    public void ryo2pos(String networkName) {
        try {
            String ryoTopic = "/SOPAC/GPS/" + networkName + "/RYO";
            String posTopic = "/SOPAC/GPS/" + networkName + "/POS";

//            ryo2pos pos = new ryo2pos(this.nbHost, this.nbPort, ryoTopic, posTopic);
//            pos.start();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /**
     *
     */
    public void singleStations() {
        try {
            String stations = (String) networkHash.get("stations");
            StringTokenizer st = new StringTokenizer(stations, ",");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                String posTopic = "/SOPAC/GPS/" + networkHash.get("network") +
                                  "/POS";
                String stationTopic = "/SOPAC/GPS/" + networkHash.get("network") +
                                      "/" + token;

                String[] argsa = {this.nbHost, this.nbPort, posTopic,
                                 stationTopic, token};
                String[] argsb = {token};

//                SingleStation single = new SingleStation(argsa,argsb);
//                single.start();
                sleep(2000);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /**
     *
     */
    public void calcDisplacements() {
        try {
            String stations = (String) networkHash.get("stations");
            StringTokenizer st = new StringTokenizer(stations, ",");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                String stationTopic = "/SOPAC/GPS/" + networkHash.get("network") +
                                      "/" + token;
                String displacementTopic = "/SOPAC/GPS/" +
                                           networkHash.get("network") +
                                           "/" + token + "/Displacements";

                CalcDisplacement disp = new CalcDisplacement(this.nbHost,
                        this.nbPort, stationTopic, displacementTopic, token);
                disp.start();
                sleep(2000);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        RunNetwork run = new RunNetwork();
        run.runNetwork("OCRTN");
//        try {
//
//            if (args.length == 0) {
//             System.out.println("Usage: java cgl.sensorgrid.sopac.gps.RunNetwork Network1 Network2 ...");
//             System.exit(0);
//         }
//         else{
//             RunNetwork run = new RunNetwork();
//             for (int i = 0; i < args.length; i++) {
//                 run.runNetwork(args[i]);
//             }
//         }
//      } catch (Exception ex) {
//          ex.printStackTrace();
//      }

    }
}
